package com.shu.scheme;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.storm.tuple.Fields;
import org.apache.storm.utils.Utils;

public class JobCustomMessageScheme implements org.apache.storm.spout.Scheme {

	private static final long serialVersionUID = 2376668657944216409L;

	public List<Object> deserialize(ByteBuffer ser) {
		try {
			String str = deserializeString(ser);
			List<Object> list = new ArrayList<Object>();
			list.add(UUID.randomUUID().toString());
			list.add(str);
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	public static String deserializeString(ByteBuffer string) throws Exception {
        if (string.hasArray()) {
            int base = string.arrayOffset();
            return new String(string.array(), base + string.position(), string.remaining());
        } else {
            return new String(Utils.toByteArray(string), "utf8");
        }
    }

	public Fields getOutputFields() {
		return new Fields("id","message");
	}

}
